var express = require('express');
var router = express.Router();
/// 引入数据库模块
var _db = require('../mysql.js');
/// 引入全局变量模块
var other = require('../other.js');

/* GET home page. */
router.get('/', function (req, res, next) {
  /// 如果没有登录就跳转登录
  if (other._username == "-null") {
    res.render('login');
    return;
  } else {
    /// 查询订单
    //SELECT * FROM orders JOIN commodity ON orders.CommodityID=commodity.CommodityID WHERE StuNum = ? ORDER BY OrderID DESC
    _db.query('SELECT DISTINCT * FROM orders JOIN commodity ON orders.CommodityID=commodity.CommodityID JOIN students ON orders.StuNum=students.StuNum WHERE orders.StuNum = ? ORDER BY OrderID DESC', [other._usernum], function (err, results) {
      if (err) {
        throw err;
      } else {
        res.render('userinfo', { title: 'Express', _username: other._username, DB: results });
      }

    });
  }
});

router.get('/cancel', function (req, res, next) {
  /// 如果没有登录就跳转登录
  if (other._username == "-null") {
    res.render('login');
    return;
  } else {
    /// 获取取消订单的订单ID
    var Orderid = req.query.id;
    _db.query('SELECT * FROM orders WHERE OrderID = ? AND StuNum = ?', [Orderid, other._usernum], function (err, results) {
      if (err) {
        throw err;
      } else {
        if (results.length < 1) {
          res.write('<head><meta charset="utf-8"/><style>body{text-align: center;margin-top: 50vh;}</style></head>');
          res.end('<span style="font-size: 50px;">你没有此订单，请不要进行卡BUG操作！</span><br /><span style="">2秒钟后自动返回。。。</span><script src="https://code.jquery.com/jquery-3.7.0.js"></script><script>setTimeout(function(){window.location.assign(\'../userinfo\');}, 2500);</script>');
        } else {
          if (results[0].State == '1') {
            /// 如果订单是已完成状态则返回错误
            res.write('<head><meta charset="utf-8"/><style>body{text-align: center;margin-top: 50vh;}</style></head>');
            res.end('<span style="font-size: 50px;">请不要进行卡BUG操作！</span><br /><span style="">2秒钟后自动返回。。。</span><script src="https://code.jquery.com/jquery-3.7.0.js"></script><script>setTimeout(function(){window.location.assign(\'../userinfo\');}, 2500);</script>');
          } else {
            /// 否则正常进行取消订单操作
            _db.query('DELETE FROM orders WHERE OrderID = ?', [Orderid], function (err, results2) {
              if (err) {
                throw err;
              } else {
                res.write('<head><meta charset="utf-8"/><style>body{text-align: center;margin-top: 50vh;}</style></head>');
                res.end('<span style="font-size: 50px;">取消订单成功！</span><br /><span style="">2秒钟后自动返回。。。</span><script src="https://code.jquery.com/jquery-3.7.0.js"></script><script>setTimeout(function(){window.location.assign(\'../userinfo\');}, 2500);</script>');
              }
            });
          }
        }
      }
    });
  }
  /// 跳转根目录
  // res.redirect('/userinfo');
});

/// 修改密码
router.post('/UpdataPwd', function (req, res, next) {
  var oldpwd = req.body.old_password;
  var newpwd1 = req.body.password;
  var newpwd2 = req.body.password2;
  /// 如果没有登录就跳转登录
  if (other._username == "-null") {
    res.render('login');
    return;
  } else {
    if (newpwd1 != newpwd2) {
      res.write('<head><meta charset="utf-8"/><style>body{text-align: center;margin-top: 50vh;}</style></head>');
      res.end('<span style="font-size: 50px;">两次密码不一样！</span><br /><span style="">2秒钟后自动返回。。。</span><script src="https://code.jquery.com/jquery-3.7.0.js"></script><script>setTimeout(function(){window.location.assign(\'../userinfo\');}, 2500);</script>');
    } else {
      _db.query('UPDATE students SET PassWord = ? WHERE StuNum = ? AND PassWord = ?', [newpwd2, other._usernum, oldpwd], function (err, results) {
        if (err) {
          throw err;
        } else {
          if (results.affectedRows < 1) {
            res.write('<head><meta charset="utf-8"/><style>body{text-align: center;margin-top: 50vh;}</style></head>');
            res.end('<span style="font-size: 50px;">请检查原密码是否正确！</span><br /><span style="">2秒钟后自动返回。。。</span><script src="https://code.jquery.com/jquery-3.7.0.js"></script><script>setTimeout(function(){window.location.assign(\'../userinfo\');}, 2500);</script>');
          } else {
            res.write('<head><meta charset="utf-8"/><style>body{text-align: center;margin-top: 50vh;}</style></head>');
            res.end('<span style="font-size: 50px;">修改密码成功！</span><br /><span style="">2秒钟后自动返回。。。</span><script src="https://code.jquery.com/jquery-3.7.0.js"></script><script>setTimeout(function(){window.location.assign(\'../userinfo\');}, 2500);</script>');
          }
        }
      });
    }
  }
})

/// 退出登录
router.get('/loginOut', function (req, res, next) {
  other._username = '-null';
  other._usernum = '-null';
  /// 暴露信息 方便其他路由调用 以防造成退出了还有登录信息
  module.exports = {
    username: other._username,
    usernum: other._usernum
  }
  /// 跳转主页
  res.redirect('/');
});

module.exports = router;
